﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Index.cshtml";
}
@section Content{
    <style>
        .btn-default .badge-danger {
            background-color: red;
        }

        .topPanel {
            height: 95px;
        }

            .topPanel .search {
                padding-top: 10px;
            }

        .zdyUL {
            list-style-type: none;
            margin: 5px 0px 0px -3px;
            padding: 0px;
        }

            .zdyUL li {
                float: left;
                list-style: none;
                height: 24px;
                line-height: 22px;
            }

                .zdyUL li a {
                    cursor: pointer;
                    padding: 5px 10px;
                    border-radius: 4px;
                }

                .zdyUL li i {
                    margin-right: 4px;
                    font-size: 13px;
                    color: #666;
                    vertical-align: middle;
                    margin-top: -1px;
                }

                .zdyUL li a:hover {
                    text-decoration: none;
                    background-color: #1ABC9C;
                    color: #fff;
                }
    </style>
}
<div class="topPanel">
    <div class="toolbar">
        <div class="btn-group">
            <a class="btn btn-primary" onclick="$.reload()"><span class="glyphicon glyphicon-refresh"></span></a>
            @*<a class="btn btn-default" onclick="btn_highSeas()">公海</a>
            <a class="btn btn-default" onclick="btn_closeOrder()">无效订单</a>*@
        </div>
    </div>
    <div class="search">
        <table>
            <tr>
                <td>
                    <div class="input-group" style="float:left; padding-right:20px; width: 200px;">
                        <select id="txt_type" class="select2">
                            <option value="">==全部==</option>
                            <option value="0">400电话</option>
                            <option value="1">销售自拓</option>
                            <option value="2">网站订单</option>
                        </select>
                    </div>
                    <div class="input-group" style="float:left;">
                        <input id="txt_keyword" type="text" class="form-control" placeholder="请输入活动名称/手机号/联系人" style="width: 200px;">
                        <span class="input-group-btn">
                            <button id="btn_search" type="button" class="btn  btn-primary"><i class="fa fa-search"></i></button>
                        </span>
                    </div>
                    <div class="clearfix"></div>
                    <div id="search_orderState" class="btn-group">
                        @*<a class="btn btn-default" data-value="0">订单指派</a>*@
                        <a class="btn btn-default active" data-value="">全部&nbsp;<span id="orderState" class="badge badge-danger"></span></a>
                        <a class="btn btn-default" data-value="1">场地推荐&nbsp;<span id="orderState1" class="badge badge-danger"></span></a>
                        <a class="btn btn-default" data-value="2">带看场地&nbsp;<span id="orderState2" class="badge badge-danger"></span></a>
                        <a class="btn btn-default" data-value="3">意向场地报价&nbsp;<span id="orderState3" class="badge badge-danger"></span></a>
                        <a class="btn btn-default" data-value="4">价格审核&nbsp;<span id="orderState4" class="badge badge-danger"></span></a>
                        <a class="btn btn-default" data-value="5">场地预定&nbsp;<span id="orderState5" class="badge badge-danger"></span></a>
                        <a class="btn btn-default" data-value="6">用印申请&nbsp;<span id="orderState6" class="badge badge-danger"></span></a>
                        <a class="btn btn-default" data-value="7">签订合同&nbsp;<span id="orderState7" class="badge badge-danger"></span></a>
                        <a class="btn btn-default" data-value="8">订单完成&nbsp;<span id="orderState8" class="badge badge-danger"></span></a>
                        <a class="btn btn-default" data-value="9">关闭订单&nbsp;<span id="orderState9" class="badge badge-danger"></span></a>
                    </div>
                </td>
            </tr>
        </table>
    </div>
</div>

<div class="gridPanel">
    <table id="gridList"></table>
    <div id="gridPager"></div>
</div>

<script>
    // from page type
    var pageType = $.request("type");
    var citys = [],
        OrderTodos = {};

    $(function () {        
        $(".select2").select2();        

        $("#search_orderState a.btn-default").click(function () {
            $("#search_orderState a.btn-default").removeClass("active");
            $(this).addClass("active");
            $('#btn_search').trigger("click");
        });

        getTodoTag(function (todos) {
            $.ajax({
                url: "/OrderFlowManage/ClientOrder/GetCitysJson",
                data: {},
                dataType: "json",
                async: false,
                success: function (data) {
                    citys = data;
                    gridList(todos);
                }
            });
        });
    })

    // 获取所有的订单信息
    function getTodoTag(callback) {
        $.ajax({
            url: "/OrderFlowManage/ClientOrder/GetCheckRecordTodoJson?pageType=" + pageType,
            data: { pageType: pageType },
            dataType: "json",
            async: false,
            success: function (data) {
                var num = 0,
                    states = {};
                for (var i = 0; i < data.length; i++) {
                    var item1 = data[i];
                    //$( + item1.F_OrderState).text(item1.F_Total);
                    if (item1.IsTag) {
                        var key = "#orderState" + item1.F_OrderState;
                        if (states[key]) {
                            states[key] += 1;
                        } else {
                            states[key] = 1;
                        }
                        num++;
                    }
                }
                //console.log(data);
                //if (data.length) {
                //    for (var x = 0; x < data.length; x++) {
                //        // data[x].F_OrderState == 1 && 
                //        if (data[x].IsTag) {
                //            num++;
                //        }
                //    }
                //}
                if (num > 0) {
                    $("#orderState").text(num);
                    for (var state in states) {
                        $(state).text(states[state]);
                    }
                }
                callback && callback(data);
            }
        });
    }

    function btn_sitesave_flow(orderId) {
        $.modalOpen({
            id: "ShowSendSite",
            title: "场地审核",
            url: "/OrderFlowManage/ClientOrder/ShowSendSite?keyValue=" + orderId,
            width: "800px",
            height: "400px",
            callBack: function (iframeId) {
                top.frames[iframeId].submitForm();
            }
        });
    }

    function btn_takelook_next_flow(orderId, formId) {
        $.modalOpen({
            id: "ShowTakelookFeedback",
            title: "带看反馈",
            url: "/OrderFlowManage/ClientOrder/ShowTakelookFeedback?orderId=" + orderId + "&formId=" + formId,
            width: "1000px",
            height: "300px",
            callBack: function (iframeId) {
                top.frames[iframeId].submitForm();
            }
        });
    }

    function gridList(todos) {
        var $gridList = $("#gridList");
        var flowStateArr = ['订单指派', '场地推荐', '带看场地', '意向场地报价', '价格审核', '场地预定', '用印申请', '签订合同', '订单完成','订单关闭'];

        var flowOrderStateArr = ['等待确认', '通过', '驳回'];
        var stateArr = ['无效', '有效', '信息不完整'];

        var num = parseInt($("#orderState").text() || 0),
            num1 = parseInt($("#orderState1").text() || 0);

        $gridList.dataGrid({
            url: "/OrderFlowManage/ClientOrder/GetGridJson",
            height: $(window).height() - 176,
            colModel: [
                { label: '单号', name: 'orderId', width: 50, sortable: false, align: 'left' },
                {
                    label: '操作', name: 'F_Id', width: 180, align: 'center', sortable: false,
                    formatter: function (cellvalue, options, rowObject) {
                        var html = '<ul class=\"zdyUL\" style=\"white-space:nowrap;\">';
                        // 当“订单管理”菜单时，只能“指派”操作
                        //if (pageType == "self" || (pageType == "all" && rowObject.F_OrderState == 0)) {
                        html += '<li>';
                        html += '<a onclick="btn_statedetail(\'' + cellvalue + '\', ' + rowObject.F_OrderState + ',' + rowObject.F_DemandId + ')"><i class=\"fa fa-eye\"></i>' + (flowStateArr[rowObject.F_OrderState] || '') + '</a>';
                        html += '</li>';
                        //}
                        if (pageType == "all" && (rowObject.F_OrderState == 0 || (rowObject.F_OrderState == 1 && rowObject.F_OrderStateProcess == -1))) {
                            html += '<li>';
                            html += '<a  onclick="btn_giveSingle(\'' + cellvalue + '\')"><i class=\"fa fa-pencil-square-o\"></i>让单</a>';
                            html += '</li>';
                        }
                        // 当“我的订单”菜单时，才能“踢单”操作
                        if (rowObject.F_SignOut != true) {
                            if ((rowObject.F_OrderState == 0 || rowObject.F_OrderState == 1 || rowObject.F_OrderState == 2)) {
                                html += '<li>';
                                html += '<a onclick="btn_giveSignOutSingle(\'' + cellvalue + '\')"><i class=\"fa fa-bullhorn\"></i>踢单</a>';
                                html += '</li>';
                            }
                        }
                        html += '</ul>';
                        return html;
                    }
                },
                {
                    label: '跟踪记录', name: 'reportState', width: 60, align: 'center', sortable: false,
                    formatter: function (cellvalue, options, rowObject) {
                        var html = '';
                        if (cellvalue == "1" && rowObject.F_OrderState!="9") {
                            html = '<span class="glyphicon glyphicon-star text-danger"></span>&nbsp;&nbsp;';
                        }
                        return html;
                        //var orderState = rowObject["F_OrderState"],
                        //    orderStateProcess = rowObject["F_OrderStateProcess"],
                        //    html = '';
                        //for (var i = 0; i < todos.length; i++) {
                        //    var item = todos[i];
                        //    if (item.IsTag && item.F_OrderId == cellvalue) {
                        //        html += '<span class="glyphicon glyphicon-star-empty text-danger"></span>&nbsp;&nbsp;';
                        //    }
                        //}
                        //if (orderState > 0 && orderState < 9) {
                        //    if (orderState == 1 && orderStateProcess == -1) {
                        //        html += '<span class="glyphicon glyphicon-star text-danger"></span>&nbsp;&nbsp;';
                        //        var _num = num,
                        //            _num1 = num1;
                        //        _num++;
                        //        _num1++;
                        //        $("#orderState").text(_num);
                        //        $("#orderState1").text(_num1);

                        //    } else {
                        //        $.ajax({
                        //            url: "/OrderFlowManage/ClientOrder/GetOrderTagsJson?orderId=" + cellvalue,
                        //            data: { orderId: cellvalue },
                        //            dataType: "json",
                        //            async: false,
                        //            success: function (data) {
                        //                for (var i = 0; i < data.SuccessStatusNum; i++) {
                        //                    html += '<span class="glyphicon glyphicon-ok text-success"></span>&nbsp;';
                        //                }
                        //                for (var i = 0; i < data.ErrorStatusNum; i++) {
                        //                    html += '<span class="glyphicon glyphicon-remove text-danger"></span>&nbsp;';
                        //                }
                        //                if (data.IsAllSuccess) {
                        //                    switch (orderState) {
                        //                        case 1:
                        //                            html += '<button onclick="btn_sitesave_flow(\'' + cellvalue + '\')" type="button" class="btn btn-danger btn-xs">审核结果</button>'
                        //                            break;
                        //                        case 2:
                        //                            html += '<button onclick="btn_takelook_next_flow(\'' + cellvalue + '\', \'' + rowObject["F_WorkFormId"] + '\')" type="button" class="btn btn-danger btn-xs">带看反馈</button>';
                        //                            break;
                        //                        default:
                        //                    }
                        //                }
                        //            }
                        //        });
                        //    }
                        //}
                    }
                },
                {
                    label: '创建时间', name: 'F_CreatorTime', width: 100, align: 'center',
                    formatter: "date", formatoptions: { srcformat: 'Y-m-d H:i', newformat: 'Y-m-d H:i' }
                },
                {
                    label: '更新时间', name: 'F_LastModifyTime', width: 100, align: 'center',
                    formatter: "date", formatoptions: { srcformat: 'Y-m-d H:i', newformat: 'Y-m-d H:i' }
                },
                {
                    label: '订单进度', name: 'F_OrderState', width: 80, align: 'left',
                    formatter: function (cellvalue, options, rowObject) {
                        // 0指派 1 场地推荐 2 带看场地 3 意向场地 4 价格谈判 5 价格谈判 6 场地预定 7 用印申请 8 签订合同 9 订单完成
                        return flowStateArr[cellvalue] || '';
                    }
                },
                {
                    label: '进度状态', name: 'F_OrderStateProcess', width: 80, align: 'left',
                    formatter: function (cellvalue, options, rowObject) {
                        // -1 等待跟进  0等待确认    1通过    2驳回
                        return cellvalue == -1 ? "等待跟进" : flowOrderStateArr[cellvalue];
                    }
                },
                {
                    label: '需求来源', name: 'F_Source', width: 50, align: 'left',
                    formatter: function (cellvalue, options, rowObject) {
                        // 订单状态:0:无效,1:有效，2：信息不完整
                        return ["400电话", "销售自拓", "网站订单","小程序订单","App订单"][cellvalue] || '';
                    }
                },
                {
                    label: '活动城市', name: 'F_ActivityCityId', width: 70, sortable: false, align: 'left',
                    formatter: function (cellvalue, options, rowObject) {
                        if (cellvalue) {
                            var arr = cellvalue.indexOf(',') == -1 ? [cellvalue] : cellvalue.split(',');
                            var result = [];
                            for (var i = 0; i < citys.length; i++) {
                                var ele = citys[i];
                                for (var j = 0; j < arr.length; j++) {
                                    var val = arr[j];
                                    if (val == ele.Value) {
                                        result.push(ele.Text);
                                    }
                                }
                            }
                            return result.toString();
                        }
                        return '';
                    }
                },
                {
                    label: '活动日期', name: 'F_ActivityStartDate', width: 180, align: 'center', sortable: false,
                    formatter: function (cellvalue, options, rowObject) {
                        var start = cellvalue ? cellvalue.split(' ')[0] : '';
                        var end = rowObject.F_ActivityEndDate ? rowObject.F_ActivityEndDate.split(' ')[0] : '';
                        return start + '~' + end;
                    }
                },
                { label: '活动主题', name: 'F_ActivityTheme', width: 250, sortable: false, align: 'left' },
                { label: '活动类型', name: 'F_ActivityTypeName', width: 150, sortable: false, align: 'left' },
                {
                    label: '预算金额(元)', name: 'F_DemandCash', width: 80, align: 'left', sortable: false,
                    formatter: function (cellvalue, options, rowObject) {
                        return (cellvalue || "").toString();
                    }
                },
                { label: '场地大小(㎡)', name: 'F_SiteArea', width: 100, sortable: false, align: 'left' },
                { label: '联系人', name: 'F_Contact', width: 60, sortable: false, align: 'left' },
                { label: '公司名称', name: 'F_ClientName', width: 180, sortable: false, align: 'left' },
                

                { label: '跟进人', name: 'F_FlowUserName', width: 60, sortable: false, align: 'left' },
                { label: '编号', name: 'F_Id', hidden: true },
                { label: '', name: 'F_DemandId', hidden: true }
            ],
            postData: {
                environment: 1,
                type: pageType
            },
            pager: "#gridPager",
            sortname: 'F_CreatorTime',
            viewrecords: true,
            sortorder: "desc",
            ondblClickRow: function (rowid, iRow, iCol, e) {
                var rowobj = $gridList.getRowData(rowid);
                btn_statedetail(rowobj["F_Id"], flowStateArr.indexOf(rowobj["F_OrderState"]), rowobj["F_DemandId"])
            }
        });
        $("#btn_search").click(function () {
            var queryJson = {
                keyword: $("#txt_keyword").val(),
                sourcetype: $("#txt_type").val(),
                orderstate: $("#search_orderState a.active").attr('data-value'),
            };
            $gridList.jqGrid('setGridParam', {
                postData: { queryJson: JSON.stringify(queryJson) },
            }).trigger('reloadGrid');
        });
    }

    function btn_add() {
        $.modalOpen({
            id: "Form",
            title: "新增 ClientOrder",
            url: "/OrderFlowManage/ClientOrder/Form",
            width: "500px",
            height: "300px",
            callBack: function (iframeId) {
                top.frames[iframeId].submitForm();
            }
        });
    }
    function btn_edit() {
        var keyValue = $("#gridList").jqGridRowValue().F_Id;
        $.modalOpen({
            id: "Form",
            title: "修改 ClientOrder",
            url: "/OrderFlowManage/ClientOrder/Form?keyValue=" + keyValue,
            width: "700px",
            height: "520px",
            callBack: function (iframeId) {
                top.frames[iframeId].submitForm();
            }
        });
    }
    function btn_delete() {
        $.deleteForm({
            url: "/OrderFlowManage/ClientOrder/DeleteForm",
            param: { keyValue: $("#gridList").jqGridRowValue().F_Id },
            success: function () {
                $.currentWindow().$("#gridList").trigger("reloadGrid");
            }
        })
    }

    // 让单
    function btn_giveSingle(keyValue) {
        $.modalOpen({
            id: "GiveSingle",
            title: "让单",
            url: "/OrderFlowManage/ClientOrder/ShowGiveSingle?keyValue=" + keyValue,
            width: "700px",
            height: "420px",
            callBack: function (iframeId) {
                top.frames[iframeId].submitForm();
            }
        });
    }

    // 踢单
    function btn_giveSignOutSingle(keyValue) {
        $.modalOpen({
            id: "GiveSingle",
            title: "踢单",
            url: "/OrderFlowManage/ClientOrder/ShowSignOutSingle?keyValue=" + keyValue,
            width: "700px",
            height: "420px",
            callBack: function (iframeId) {
                top.frames[iframeId].submitForm();
            }
        });
        //$.modalConfirm('您确定要将此订单转为合同单吗？', function (flag, index) {
        //    if (flag)  $.modalConfirmClose(index);
        //});
    }

    // 公海
    function btn_highSeas() {
        var id = RndNum();
        $.addTab({
            href: '/OrderFlowManage/ClientOrder/ShowHighSeas',
            text: '公海',
            id: id,
            index: 998
        });
    }

    // 无效订单
    function btn_closeOrder() {
        var id = RndNum();
        $.addTab({
            href: '/OrderFlowManage/ClientOrder/CloseOrderIndex',
            text: '无效订单',
            id: id,
            index: 997
        });
    }


    function btn_detail(id, type, F_DemandId) {
        var frameId = $.currentWindow().frameElement.id;
        var isread = pageType == "self" ? "1" : 0;
        $.addTab({
            href: '/OrderFlowManage/ClientOrder/Details?pageType=' + pageType + '&orderid=' + id + '&type=' + type + '&isread=' + isread + '&frameid=' + frameId + '&F_DemandId=' + F_DemandId,
            text: '需求详细',
            id: id,
            index: 999
        });
    }

    function btn_statedetail(id, type, F_DemandId) {
        if (type < 0) {
            $.modalAlert("订单状态错误，请联系管理员。", "error");
        } else if (type == 0) {
            $.modalOpen({
                id: "ShowGiveSingle",
                title: "订单指派",
                url: "/OrderFlowManage/ClientOrder/ShowGiveSingle?keyValue=" + id,
                width: "700px",
                height: "420px",
                callBack: function (iframeId) {
                    top.frames[iframeId].submitOrderForm();
                }
            });
        } else {
            btn_detail(id, type, F_DemandId);
        }
    }

    function reloadPage() {
        location.href = location.href;
    }

</script>
